package com.enation.app.javashop.core.payment.plugin.alipay.executor;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.RequestParametersHolder;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.enation.app.javashop.core.payment.model.vo.Form;
import com.enation.app.javashop.core.payment.model.vo.PayBill;
import com.enation.app.javashop.core.payment.plugin.alipay.AlipayConfig;
import com.enation.app.javashop.core.payment.plugin.alipay.AlipayPluginConfig;
import com.enation.app.javashop.framework.util.JsonUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fk
 * @version v1.0
 * @Description: 支付宝wap端
 * @date 2018/4/1714:55
 * @since v7.0.0
 */
@Service
public class AliPayPaymentWapExecutor extends AlipayPluginConfig {


    /**
     * 支付
     *
     * @param bill
     * @return
     */
    public Form onPay(PayBill bill) {

        String form = "";
        try {
            super.setConfig(bill);

            AlipayClient alipayClient = new JavaShopPayClient(AlipayConfig.gatewayUrl, AlipayConfig.appId, AlipayConfig.merchantPrivateKey, "json", AlipayConfig.charset, AlipayConfig.alipayPublicKey, AlipayConfig.signType);
            //设置请求参数
            AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
            alipayRequest.setNotifyUrl(this.getCallBackUrl(bill.getTradeType(), bill.getClientType()));
            alipayRequest.setReturnUrl(this.getReturnUrl(bill));

            // 商户网站订单
            String outTradeNo = bill.getOutTradeNo();
            Double payMoney = bill.getOrderPrice();

            // 订单名称
            String subject = siteName + "订单";

            Map<String, String> sParaTemp = new HashMap<String, String>(16);
            sParaTemp.put("out_trade_no", outTradeNo);
            sParaTemp.put("product_code", "FAST_INSTANT_TRADE_PAY");
            sParaTemp.put("total_amount", payMoney + "");
            sParaTemp.put("subject", subject);
            sParaTemp.put("body", "");

            ObjectMapper json = new ObjectMapper();
            //填充业务参数
            alipayRequest.setBizContent(json.writeValueAsString(sParaTemp));
            return JsonUtil.jsonToObject(alipayClient.pageExecute(alipayRequest).getBody(), Form.class);

        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return null;
    }

}
